package timing.ukulele.portal.controller;

import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RestController;
import timing.ukulele.common.data.ResponseData;
import timing.ukulele.data.portal.data.AntMenuTree;
import timing.ukulele.data.portal.view.AppInfoVO;
import timing.ukulele.data.portal.view.RoleVO;
import timing.ukulele.facade.portal.mvc.IndexControllerFacade;
import timing.ukulele.portal.persistent.SysParam;
import timing.ukulele.portal.persistent.SysUser;
import timing.ukulele.portal.service.AntMenuService;
import timing.ukulele.portal.service.SysParamService;
import timing.ukulele.portal.service.SysRoleService;
import timing.ukulele.portal.service.SysUserService;
import timing.ukulele.web.util.BeanUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@RestController
public class IndexController implements IndexControllerFacade {
    private final AntMenuService menuService;
    private final SysParamService paramService;
    private final SysRoleService roleService;
    private final SysUserService userService;

    public IndexController(AntMenuService menuService, SysUserService userService,
                           SysParamService paramService, SysRoleService roleService) {
        this.menuService = menuService;
        this.userService = userService;
        this.paramService = paramService;
        this.roleService = roleService;
    }

    @Override
    public ResponseData<AppInfoVO> current(String username, Long tenantId, Integer type, String module) {
        AppInfoVO vo = new AppInfoVO();
        String APP_NAME = "APP_NAME";
        String APP_DESCRIPTION = "APP_DESCRIPTION";
        List<SysParam> paramList = paramService.lambdaQuery()
                .eq(SysParam::getKey, APP_NAME)
                .or()
                .eq(SysParam::getKey, APP_DESCRIPTION)
                .select(SysParam::getKey, SysParam::getValue)
                .list();
        if (!CollectionUtils.isEmpty(paramList)) {
            HashMap<String, Object> app = new HashMap<>(4);
            Map<String, SysParam> paramMap = paramList.stream().collect(Collectors.toMap(SysParam::getKey, Function.identity(), (key1, key2) -> key2));
            SysParam name = paramMap.get(APP_NAME);
            if (name != null)
                app.put("name", name.getValue());
            SysParam desc = paramMap.get(APP_DESCRIPTION);
            if (desc != null)
                app.put("description", desc.getValue());
            vo.setApp(app);
        }
        SysUser user = userService.getUserByUserName(username);
        if (user != null ) {
            user.setPassword(null);
            Map<String, Object> userMap = BeanUtil.beanToMap(user);
            vo.setUser(userMap);
            List<RoleVO> userRoles = roleService.getUserRole(user.getId(), tenantId);
            if (!CollectionUtils.isEmpty(userRoles)) {
                List<Long> userRoleIds = userRoles.stream().map(RoleVO::getId).collect(Collectors.toList());
                List<AntMenuTree> menuList = menuService.getMenuTreeByRole(user.getLabel(), userRoleIds, type, false, module);
                vo.setMenu(menuList);
            }
        }
        return ResponseData.success(vo);
    }

    @Override
    public ResponseData<List<AntMenuTree>> currentMenu(String username, Long tenantId, Integer type, String module) {
        SysUser user = userService.getUserByUserName(username);
        if (user != null ) {
            List<RoleVO> userRoles = roleService.getUserRole(user.getId(), tenantId);
            if (!CollectionUtils.isEmpty(userRoles)) {
                List<Long> userRoleIds = userRoles.stream().map(RoleVO::getId).collect(Collectors.toList());
                List<AntMenuTree> menuList = menuService.getMenuTreeByRole(user.getLabel(), userRoleIds, type, false, module);
                return ResponseData.success(menuList);
            }
        }
        return ResponseData.success();
    }
}
